package com.caishi.lkx.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.map.MapUtil;
import com.alibaba.fastjson.JSONObject;
import com.caishi.lkx.common.ienum.DeviceType;
import com.caishi.lkx.user.dto.UserDeviceState;
import com.caishi.lkx.user.mapper.UserDeviceMapper;
import com.caishi.lkx.user.model.UserDeviceModel;
import com.caishi.lkx.user.service.IUserDeviceService;
import com.caishi.lkx.user.vo.UserDevicePushResult;
import com.zzw.common.entity.IBSDMapper;
import com.zzw.common.entity.impl.BSDEntity;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
//@DubboService(interfaceClass = IUserDeviceBaseService.class)
public class UserDeviceServiceImpl implements IUserDeviceService {
    @Resource
    private UserDeviceMapper userDeviceMapper;

    @Override
    public IBSDMapper<UserDeviceModel, String> getMapper() {
        return userDeviceMapper;
    }

    @Override
    public void settingUserDevice(String userId, boolean login, DeviceType deviceType, UserDeviceState deviceIdData) {
        UserDeviceModel oldData = userDeviceMapper.selectById(userId);
        if (oldData == null) {
            oldData = new UserDeviceModel();
            oldData.setId(userId);
            oldData.setData(new HashMap<>());
            userDeviceMapper.insert(oldData);
        }
        Map<String, JSONObject> deviceData = oldData.getData();
        UserDeviceState userDeviceState = new UserDeviceState();
        if (deviceIdData != null) {
            BeanUtil.copyProperties(deviceIdData, userDeviceState, CopyOptions.create().setIgnoreNullValue(true));
        }
        userDeviceState.setLs(login);
        deviceData.put(deviceType.name(), JSONObject.parseObject(JSONObject.toJSONString(userDeviceState)));
        oldData.setData(deviceData);
        userDeviceMapper.updateById(oldData);
    }

    @Override
    public List<UserDevicePushResult> appPushUserDevice(String userId) {
        return appPushUserDevice(Collections.singletonList(userId)).get(0);
    }

    @Override
    public List<List<UserDevicePushResult>> appPushUserDevice(List<String> userIds) {
        Map<String, UserDeviceModel> data = userDeviceMapper.selectBatchIds(userIds).stream()
                .collect(Collectors.toMap(BSDEntity::getId, v -> v));
        return userIds.stream().map(userId -> {
            UserDeviceModel d = data.get(userId);
            if (d == null || MapUtil.isEmpty(d.getData())) {
                return Collections.<UserDevicePushResult>emptyList();
            }
            Map<String, JSONObject> deviceData = d.getData();
//            return deviceData.keySet().stream()
//                    .filter(key -> key.equals(DeviceType.android.name()) || key.equals(DeviceType.ios.name()))
//                    .filter(key ->  BeanUtil.mapToBean(deviceData.get(key), UserDeviceState.class, true, CopyOptions.create()) .isLs())
//                    //.map(key -> new UserDevicePushResult(BeanUtil.mapToBean(deviceData.get(key), UserDeviceState.class, true, CopyOptions.create()).getAid(), key))
//                    .collect(Collectors.toList());
            return null;
        }).collect(Collectors.toList());
    }

//    @Override
//    public void rpcSettingUserDevice(String userId, boolean login, RpcDeviceType rpcDeviceType, RpcUserDeviceState deviceIdData) {
//        var userDeviceState = BeanUtil.copyProperties(deviceIdData, UserDeviceState.class);
//        this.settingUserDevice(userId, login, DeviceType.rpcType(rpcDeviceType), userDeviceState);
//    }
}
